package com.mlizhi.modules.spec.detect;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import com.mlizhi.kingdom.R;
import com.mlizhi.base.MlzApplication;
import com.mlizhi.base.Session;
import com.mlizhi.utils.Constants;
import com.mlizhi.widgets.cursor4water.WaterRangeView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class SpecDetectResultActivity extends Activity {

	private ImageView detectResultIv;
	//部位
	private TextView positionText;
	//性别
	private TextView sexText;
	//年龄
	private TextView ageText;
	//肤质类型
	private TextView skinText;
	
	private Session mSession;
	private MlzApplication mlzApplication = null;
	private WaterRangeView waterRangeView;
	
	private TextView compareAgeTv;
	
	private TextView conditionValue;//肤质分析
	private TextView recommandText;//护肤建议
	private TextView nurserTypeTextView;//护理前后
	private int tempNurserType;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_spec_detect_result);
		mlzApplication = (MlzApplication) getApplication();
		mSession = Session.get(getApplicationContext());
		detectResultIv = (ImageView) findViewById(R.id.id_detect_result_back);
		positionText = (TextView) findViewById(R.id.position_text);
		sexText = (TextView) findViewById(R.id.sex_text);
		ageText = (TextView) findViewById(R.id.age_text);
		skinText = (TextView) findViewById(R.id.skin_text);
		
		waterRangeView = (WaterRangeView) findViewById(R.id.id_water_range_view);
		compareAgeTv = (TextView) findViewById(R.id.id_nurser_result_compare_value);
		
		conditionValue = (TextView) findViewById(R.id.id_skan_condition_value);
		recommandText = (TextView) findViewById(R.id.id_detect_result_recommand_text);
		nurserTypeTextView = (TextView) findViewById(R.id.id_nurser_resutl_label);
		
		String briday = mSession.getUserBriday();
		String briday_year = "1990";
		if(briday != null && !"".equals(briday)) {
			briday_year = briday.substring(0,4);
		}
		String now_year =  new SimpleDateFormat("yyyy-MM-dd").format(new Date()).substring(0,4);
		int age = 25;
		try{
			age = Integer.parseInt(now_year) - Integer.parseInt(briday_year);
		}catch(Exception e){
//			e.printStackTrace();
			age = 25;
		}
		ageText.setText(String.valueOf(age));
		String sex = "";
		if(Constants.USER_SEX_FEMALE.equals(mSession.getUserSex())){
			sex = getString(R.string.user_pre_setting_sex_female);
		}else if(Constants.USER_SEX_MALE.equals(mSession.getUserSex())){
			sex = getString(R.string.user_pre_setting_sex_male);
		}
		sexText.setText(sex);
		String skinType = getString(R.string.user_pre_setting_skin_type_unknow);
		if(Constants.USER_SKIN_TYPE_DRY.equals(mSession.getUserSkinType())) {
			skinType = getString(R.string.user_pre_setting_skin_type_dry);
		} else if(Constants.USER_SKIN_TYPE_OILY.equals(mSession.getUserSkinType())) {
			skinType = getString(R.string.user_pre_setting_skin_type_oil);
		}else if(Constants.USER_SKIN_TYPE_HYBRID.equals(mSession.getUserSkinType())) {
			skinType = getString(R.string.user_pre_setting_skin_type_hybird);
		}else if(Constants.USER_SKIN_TYPE_SENSITIVE.equals(mSession.getUserSkinType())) {
			skinType = getString(R.string.user_pre_setting_skin_type_sensitive);
		}else if(Constants.USER_SKIN_TYPE_UNKNOW.equals(mSession.getUserSkinType())) {
			skinType = getString(R.string.user_pre_setting_skin_type_unknow);
		}
		skinText.setText(skinType);
//		年龄 ：20岁    性别 ： 女    性质 ： 中性肌肤
		
		
//		String waterValue = mlzApplication.getGlobalVariable("current_water_value");
//		if(waterValue != null && !"".equals(waterValue) && waterValue.length() > 4){
//			waterValue = waterValue.substring(0,4);
//		}else{
//			waterValue = "0.0";
//		}
		
		Intent intent = getIntent();
		float progress = intent.getFloatExtra("current_water_value", Constants.BODY_PART_MIN);
//		try {
//			progress = Float.parseFloat(waterValue);
//		} catch (NumberFormatException e) {
//			progress = Constants.BODY_PART_MIN;
//		}
		waterRangeView.setProgress(progress, Constants.BODY_PART_MIN, Constants.BODY_PART_MAX);
		
		tempNurserType = intent.getIntExtra("nurser_type", 0);
		if(tempNurserType == Constants.NURSER_TYPE_PRE){
			nurserTypeTextView.setText(R.string.flag4pre);
		}else if(tempNurserType == Constants.NURSER_TYPE_POST){
			nurserTypeTextView.setText(R.string.flag4post);
		}
		
		String currentPartType = mlzApplication.getGlobalVariable("current_part_type");
		int partType = Constants.BODY_PART_HAND;
		try {
			partType = Integer.parseInt(currentPartType);
		} catch (NumberFormatException e) {
			partType = Constants.BODY_PART_HAND;
		}
		String suggestArr[] = null;
		String[] analysisArr = null;
		if(partType == Constants.BODY_PART_FACE){
			positionText.setText(R.string.bodyParts4face);
			waterRangeView.setMeasureWidth(Constants.BODY_PART_MIN, Constants.BODY_PART_FACE_MORMAL_MIN, Constants.BODY_PART_FACE_MORMAL_MAX, Constants.BODY_PART_MAX, true);
			suggestArr = getResources().getStringArray(R.array.suggest_face);
			
			if(Constants.BODY_PART_MIN <= progress && progress <Constants.BODY_PART_FACE_MORMAL_MIN){
				analysisArr = getResources().getStringArray(R.array.analysis_dry);
			}else if(Constants.BODY_PART_FACE_MORMAL_MIN <= progress && progress <Constants.BODY_PART_FACE_MORMAL_MAX){
				analysisArr = getResources().getStringArray(R.array.analysis_normal);
			}else if(Constants.BODY_PART_FACE_MORMAL_MAX <= progress && progress <Constants.BODY_PART_MAX){
				analysisArr = getResources().getStringArray(R.array.analysis_moist);
			}
			
			int compareAgeValue = (int) (progress - (Constants.BODY_PART_FACE_MORMAL_MAX+Constants.BODY_PART_FACE_MORMAL_MIN)*0.5f);
			if(compareAgeValue >= 0){
				String largeString = String.format(getResources().getString(R.string.detect_result_compare_age_large), compareAgeValue);
				compareAgeTv.setText(largeString);
			}else{
				String smallString = String.format(getResources().getString(R.string.detect_result_compare_age_small), (0-compareAgeValue));
				compareAgeTv.setText(smallString);
			}
			
		}else if(partType == Constants.BODY_PART_EYE){
//			bodyPartImage.setImageResource(R.drawable.ic_part_eye_pressed);
			positionText.setText(R.string.bodyParts4eye);
			waterRangeView.setMeasureWidth(Constants.BODY_PART_MIN, Constants.BODY_PART_EYE_MORMAL_MIN, Constants.BODY_PART_EYE_MORMAL_MAX, Constants.BODY_PART_MAX, true);
			suggestArr = getResources().getStringArray(R.array.suggest_eye);
			
			if(Constants.BODY_PART_MIN <= progress && progress <Constants.BODY_PART_EYE_MORMAL_MIN){
				analysisArr = getResources().getStringArray(R.array.analysis_dry);
			}else if(Constants.BODY_PART_EYE_MORMAL_MIN <= progress && progress <Constants.BODY_PART_EYE_MORMAL_MAX){
				analysisArr = getResources().getStringArray(R.array.analysis_normal);
			}else if(Constants.BODY_PART_EYE_MORMAL_MAX <= progress && progress <Constants.BODY_PART_MAX){
				analysisArr = getResources().getStringArray(R.array.analysis_moist);
			}
			
			int compareAgeValue = (int) (progress - (Constants.BODY_PART_EYE_MORMAL_MAX+Constants.BODY_PART_EYE_MORMAL_MIN)*0.5f);
			if(compareAgeValue >= 0){
				String largeString = String.format(getResources().getString(R.string.detect_result_compare_age_large), compareAgeValue);
				compareAgeTv.setText(largeString);
			}else{
				String smallString = String.format(getResources().getString(R.string.detect_result_compare_age_small), (0-compareAgeValue));
				compareAgeTv.setText(smallString);
			}
			
		}else if(partType == Constants.BODY_PART_HAND){
//			bodyPartImage.setImageResource(R.drawable.ic_part_hand_pressed);
			positionText.setText(R.string.bodyParts4hand);
			waterRangeView.setMeasureWidth(Constants.BODY_PART_MIN, Constants.BODY_PART_HAND_MORMAL_MIN, Constants.BODY_PART_HAND_MORMAL_MAX, Constants.BODY_PART_MAX, true);
			suggestArr = getResources().getStringArray(R.array.suggest_hand);
			
			if(Constants.BODY_PART_MIN <= progress && progress <Constants.BODY_PART_HAND_MORMAL_MIN){
				analysisArr = getResources().getStringArray(R.array.analysis_dry);
			}else if(Constants.BODY_PART_HAND_MORMAL_MIN <= progress && progress <Constants.BODY_PART_HAND_MORMAL_MAX){
				analysisArr = getResources().getStringArray(R.array.analysis_normal);
			}else if(Constants.BODY_PART_HAND_MORMAL_MAX <= progress && progress <Constants.BODY_PART_MAX){
				analysisArr = getResources().getStringArray(R.array.analysis_moist);
			}
			
			int compareAgeValue = (int) (progress - (Constants.BODY_PART_HAND_MORMAL_MAX+Constants.BODY_PART_HAND_MORMAL_MIN)*0.5f);
			if(compareAgeValue >= 0){
				String largeString = String.format(getResources().getString(R.string.detect_result_compare_age_large), compareAgeValue);
				compareAgeTv.setText(largeString);
			}else{
				String smallString = String.format(getResources().getString(R.string.detect_result_compare_age_small), (0-compareAgeValue));
				compareAgeTv.setText(smallString);
			}
			
		}else if(partType == Constants.BODY_PART_NECK){
//			bodyPartImage.setImageResource(R.drawable.ic_part_neck_pressed);
			positionText.setText(R.string.bodyParts4neck);
			waterRangeView.setMeasureWidth(Constants.BODY_PART_MIN, Constants.BODY_PART_NECK_MORMAL_MIN, Constants.BODY_PART_NECK_MORMAL_MAX, Constants.BODY_PART_MAX, true);
			suggestArr = getResources().getStringArray(R.array.suggest_neck);
			
			if(Constants.BODY_PART_MIN <= progress && progress <Constants.BODY_PART_NECK_MORMAL_MIN){
				analysisArr = getResources().getStringArray(R.array.analysis_dry);
			}else if(Constants.BODY_PART_NECK_MORMAL_MIN <= progress && progress <Constants.BODY_PART_NECK_MORMAL_MAX){
				analysisArr = getResources().getStringArray(R.array.analysis_normal);
			}else if(Constants.BODY_PART_NECK_MORMAL_MAX <= progress && progress <Constants.BODY_PART_MAX){
				analysisArr = getResources().getStringArray(R.array.analysis_moist);
			}
			
			int compareAgeValue = (int) (progress - (Constants.BODY_PART_NECK_MORMAL_MAX+Constants.BODY_PART_NECK_MORMAL_MIN)*0.5f);
			if(compareAgeValue >= 0){
				String largeString = String.format(getResources().getString(R.string.detect_result_compare_age_large), compareAgeValue);
				compareAgeTv.setText(largeString);
			}else{
				String smallString = String.format(getResources().getString(R.string.detect_result_compare_age_small), (0-compareAgeValue));
				compareAgeTv.setText(smallString);
			}
		}
		if(analysisArr != null && analysisArr.length >0) {
			int index = Math.abs(new Random().nextInt())%analysisArr.length;
			conditionValue.setText(analysisArr[index]);
		}
		
		if(suggestArr != null && suggestArr.length >0) {
			int index = Math.abs(new Random().nextInt())%suggestArr.length;
			recommandText.setText(suggestArr[index]);
		}
	}

	public void onSpecViewClick(View view) {
		switch (view.getId()) {
		case R.id.id_detect_result_back:
			this.finish();
			break;

		default:
			break;
		}
	}
	
	
}
